package com.huaruan.food.biz.admin.food.imp;

import java.util.List;

import com.huaruan.food.biz.admin.food.IFoodTypeManager;
import com.huaruan.food.dao.IDao;
import com.huaruan.food.entity.Food;
import com.huaruan.food.entity.FoodType;
import com.huaruan.food.javabean.PageObj;

public class FoodTypeManagerImp implements IFoodTypeManager {
	private IDao dao;
	public void setDao(IDao dao){
		this.dao = dao;
	}
	
	@Override
	public List<FoodType> getAllFoodType() {
		return dao.find(new FoodType());
	}

	@Override
	public List<FoodType> ckFoodType(String name) {
		return dao.findByNamedQuery("ckFoodType", new  Object[]{name});
	}

	@Override
	public boolean addFoodType(FoodType foodType) {
		return dao.addObj(foodType);
	}

	@Override
	public PageObj<FoodType> findFoodTypes(PageObj<FoodType> pageObj) {
		List<FoodType> result = dao.find(pageObj.getQueryObj(),
				pageObj.getCurrPage(), pageObj.getRows());
		if (null != result && result.size() > 0) {
			pageObj.setResults(result);
			pageObj.setAllRows(dao.listCount(pageObj.getQueryObj()));
		}
		return pageObj;
	}

	@Override
	public int delFoodType(String[] foodTypeIds) {
		StringBuffer hql=new StringBuffer("delete from  FoodType where id = ");
		for (int i = 0; i < foodTypeIds.length; i++) { 
	       if (foodTypeIds.length-1==i) { 
	        	hql.append(Integer.parseInt(foodTypeIds[i]) ); 
	       } else{ 
	    	   hql.append(Integer.parseInt(foodTypeIds[i]) + " or id="); 
	       } 
	    } 
		return dao.delOrUpdate(hql.toString());
	}

	@Override
	public boolean updateFoodType(FoodType foodType) {
			return dao.delOrUpdateByNamed("updateFoodType", foodType.getName(),foodType.getId())>0;
	}

}
